﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AssetStudio
{
    //读取TerrainData的#16 号数据
    public class DetailPatch
    {
        public AABB aabb;
        public DetailPatch(ObjectReader reader)
        {
            aabb = new AABB(reader);
            var layer = reader.ReadInt32();
            //layerIndex
            for (int i = 0; i < layer; i++)
            {
            }
            var objNum = reader.ReadInt32();
            for(int i = 0;i < objNum; i++)
            {
            }
        }
    }
    [MessagePack.MessagePackObject(true)]
    public class Heightmap
    {
        public List<short> m_Heights;
        public List<float> m_PrecomputedError;
        public List<float> m_MinMaxPatchHeights;
        public int m_Resolution;
        public int m_Levels;
        public Vector3 m_Scale;
        [MessagePack.IgnoreMember]
        public List<PPtr<Object>> m_PreloadShaders;
        public Heightmap() { }
        public Heightmap(ObjectReader reader)
        {
            var num = reader.ReadInt32();
            m_Heights = new List<short>(num);
            for (int i = 0; i < num; i++)
            {
                m_Heights.Add(reader.ReadInt16());
            }
            reader.AlignStream();

            var hole = reader.ReadInt32();
            for (var i = 0; i < hole; i++) { }
            reader.AlignStream();
            var holdLod = reader.ReadInt32();
            for (var i = 0; i < holdLod; i++) { }
            reader.AlignStream();
            var m_EnableHolesTextureCompression = reader.ReadBoolean();
            reader.AlignStream();

            //4字节无需对齐
            //1字节需要对齐 2字节需要对齐
            var error = reader.ReadInt32();
            m_PrecomputedError = new List<float>(error);
            for(var i = 0; i < error; i++)
            {
                m_PrecomputedError.Add(reader.ReadSingle());
            }
            m_MinMaxPatchHeights = new List<float>();
            var minNum = reader.ReadInt32();
            for (var i = 0; i < minNum; i++)
            {
                m_MinMaxPatchHeights.Add(reader.ReadSingle());
            }
            m_Resolution = reader.ReadInt32();
            m_Levels = reader.ReadInt32();
            m_Scale = reader.ReadVector3();
            var shaderNum = reader.ReadInt32();
            m_PreloadShaders = new List<PPtr<Object>>(shaderNum);
            for (var i = 0; i < shaderNum; i++)
            {
                m_PreloadShaders.Add(new PPtr<Object>(reader));
            }
        }
    }
    [MessagePack.MessagePackObject(false)]
    //导出地形高度数据
    public class TerrainData : NamedObject
    {
        [MessagePack.IgnoreMember]
        public List<PPtr<Object>> terrainLayer;
        [MessagePack.IgnoreMember]
        public List<PPtr<Object>> alphaLayer;
        [MessagePack.Key(0)]
        public int m_AlphamapResolution;
        [MessagePack.Key(1)]
        public int m_BaseMapResolution;
        [MessagePack.IgnoreMember]
        public List<DetailPatch> details;
        [MessagePack.Key(2)]
        public int m_PatchCount;
        [MessagePack.Key(3)]
        public int m_PatchSamples;
        [MessagePack.Key(4)]
        public Color WavingGrassTint;
        [MessagePack.Key(5)]
        public float m_WavingGrassStrength;
        [MessagePack.Key(6)]
        public float m_WavingGrassAmount;
        [MessagePack.Key(7)]
        public float m_WavingGrassSpeed;
        [MessagePack.IgnoreMember]
        public PPtr<Object> m_DetailBillboardShader, m_DetailMeshLitShader, m_DetailMeshGrassShader;
        [MessagePack.Key(8)]
        public Heightmap heightMap;
        //SplatData TerrainArray
        public TerrainData() { }
        public TerrainData(ObjectReader reader) : base(reader)
        {
            var size = reader.ReadInt32();
            terrainLayer = new List<PPtr<Object>>();
            for (int i = 0; i < size; i++)
            {
                var layer = new PPtr<Object>(reader);
                terrainLayer.Add(layer);
            }
            var alpha = reader.ReadInt32();
            alphaLayer = new List<PPtr<Object>>();
            for (int i = 0;i < alpha; i++)
            {
                var layer = new PPtr<Object>(reader);
                alphaLayer.Add(layer);
            }
            m_AlphamapResolution = reader.ReadInt32();
            m_BaseMapResolution = reader.ReadInt32();
            //1024草数据
            details = new List<DetailPatch>();
            var detailSize = reader.ReadInt32();
            for(int i = 0;  i <detailSize; i++)
            {
                details.Add(new DetailPatch(reader));
            }
            var protoNum = reader.ReadInt32();
            for(var i = 0; i < protoNum; i++)
            {
            }
            m_PatchCount = reader.ReadInt32();
            m_PatchSamples = reader.ReadInt32();
            var rotSize  = reader.ReadInt32();
            for (var i = 0; i < rotSize; i++)
            {
            }
            WavingGrassTint = reader.ReadColor4();
            m_WavingGrassStrength = reader.ReadSingle();
            m_WavingGrassAmount = reader.ReadSingle();
            m_WavingGrassSpeed = reader.ReadSingle();

            m_DetailBillboardShader = new PPtr<Object>(reader);
            m_DetailMeshLitShader = new PPtr<Object>(reader);
            m_DetailMeshGrassShader = new PPtr<Object>(reader);

            var treeNum = reader.ReadInt32();
            for(var i =0; i < treeNum; i++)
            {
            }
            var treeProto = reader.ReadInt32();
            for(var i = 0; i < treeProto; i++)
            {
            }
            var preload = reader.ReadInt32();
            for (var i = 0; i < preload; i++)
            {
            }
            heightMap = new Heightmap(reader);
        }
    }
}
